	<?php

	//--------------------------------------------
	//	THIS CLASS CONNECTS TO THE DATABASE(S).
	
	class dbConnect
	{
	
		//-----------------------------------------
		//	Get_Category retrieves the information
		//	from the database regarding test table
		//	tuples of category and sub_category.

		function Get_Category( $params, $SQL )
		{
			$Params = $params;

			$conn = @pg_connect( $params ); 
			
			$R1 = pg_exec( $conn, $SQL );
	
			if( !$R1 )
			{
				echo '<br/>Error connecting to the database.<br/>';
			}
	
			$num1 = @pg_numrows( $R1 ); 
			$dbname = @pg_dbname( $conn );

			echo '<table align="center" border="1" bordercolor="#333333" cellpadding="1" cellspacing="0" width="100%">';
		
			echo '<tr>';
			echo "<td class=\"rowheader\">DATABASE:</td><td class=\"rowheader\">$dbname</td>";
			echo '</tr>';
		
			$this->get_new_hardware( $params );
			
			$this->get_new_firmware( $params );
		
			for( $i = 0; $i < $num1; $i++ ) 
			{ 
				echo '<tr>';
			  	$r1 = @pg_fetch_row( $R1, $i );
				
					echo '<td width="100" class="rowheader">';
					echo "$r1[0] &nbsp;"; 
					echo '</td>';
		
					echo '<td width="500" class="row">&nbsp;';
					$this->create_cat_table( $r1[0], $params );
					echo '</td>';
			
			}// end for-loop

			echo '</tr>';
			echo '</table>';

		}// end function Get_Categories()



		function get_new_hardware( $params )
		{
			$sql = "SELECT DISTINCT \"group\" FROM category_table WHERE \"group\" = 'New_Hardware';";
			
			$conn = @pg_connect( $params );
			
			$result = @pg_exec( $conn, $sql );
			
			if( !$result )
			{
				echo '<br/>Error connecting to the database.<br/>';
			}
			
			$num = @pg_numrows( $result );
			 
			 for( $i = 0; $i < $num; $i++ ) 
			{ 
				echo '<tr>';
			  	$r = @pg_fetch_row( $result, $i );
				
				foreach( $r as $k => $text )
				{
					echo '<td width="100" class="rowheader">';
					echo "$text &nbsp;"; 
					echo '</td>';
					
					echo '<td width="600" class="row">';
					$this->create_cat_table( $text, $params );
					echo '</td>';

				}// end inner for-loop
 
			}// end for-loop
		
		}// end function get_new_hardware();




		function get_new_firmware( $params )
		{
			$sql = "SELECT DISTINCT \"group\" FROM category_table WHERE \"group\" = 'New_Firmware';";
			
			$conn = @pg_connect( $params );
			
			$result = @pg_exec( $conn, $sql );
			
			if( !$result )
			{
				echo '<br/>Error connecting to the database.<br/>';
			}
			
			$num = @pg_numrows( $result );
			 
			 for( $i = 0; $i < $num; $i++ ) 
			{ 
				echo '<tr>';
			  	$r = @pg_fetch_row( $result, $i );
				
				foreach( $r as $k => $text )
				{
					echo '<td width="100" class="rowheader">';
					echo "$text&nbsp;"; 
					echo '</td>';

					echo '<td width="600" class="row">';
					$this->create_cat_table( $text, $params );
					echo '</td>';
															
				}// end inner for-loop
 
			}// end for-loop
		
		}// end function get_new_hardware();



		function create_cat_table( $cat, $params )
		{
			$sql = "SELECT DISTINCT category FROM category_table WHERE \"group\" = '" . $cat . "' ORDER BY category;";

			$con = @pg_connect( $params ); 
			
			$result = @pg_exec( $con, $sql );
	
			if( !$result )
			{
				echo '<br/>Error connecting to the database.<br/>';
			}
	
			$num = @pg_numrows( $result );

			$dbname = @pg_dbname( $con );

			echo '<table align="left" border="0" cellpadding="0" cellspacing="0" width="700">';
			
			for( $i = 0; $i < $num; $i++ ) 
			{ 
				$r = @pg_fetch_row( $result, $i );
				
				foreach( $r as $k => $text  )
				{
						echo '<tr>';
						echo '<td width="150">';
						echo "&nbsp;" . $text . "</a>&nbsp;";
						echo '</td>';

						echo '<td width="500" class="row">';
						$this->create_sub_table( $text, $params );
						echo '</td>';


						echo '</tr>';
		
				}// end inner for-loop
			
			}// end for-loop		
		
			echo '</table>';
			
			$sql = " ";


		}// end function create_cat_table()



		function create_sub_table( $cat, $params )
		{
			$sql = "SELECT sub_category, status, percent, category FROM category_table WHERE category = '" . $cat . "' ORDER BY CATEGORY, SUB_CATEGORY, STATUS, PERCENT";

			$con = @pg_pconnect( $params ); 
			
			$result = @pg_exec( $con, $sql );
	
			if( !$result )
			{
				echo '<br/>Error connecting to the database.<br/>';
			}
	
			$num = @pg_numrows( $result );

			$dbname = @pg_dbname( $con );

			echo '<table align="left" class="rowsubcat" cellpadding="3" cellspacing="0" width="500">';
			
			for( $i = 0; $i < $num; $i++ ) 
			{ 
				$r = @pg_fetch_row( $result, $i );
				
				foreach( $r as $k => $text  )
				{
					if( $k == 0 )
					{
						echo '<tr>';
 						echo '<td width="130">';
						echo "<a href=display_test_data.php?DBNAME=$dbname&CAT=$r[3]&SUBCAT=$text>" . $text . "</a>&nbsp;"; 
						echo '</td>';
					}

					if( $k == 1 )
					{
						if( $text == 0 )
						{
							echo '<td width="100" bgcolor="red">';
							echo "FAIL";
							echo '</td>';
					  	}

						if( $text == 1 )
						{
							echo '<td width="100" bgcolor="lime">';
							echo "PASS";
							echo "</td>";	
						}

						if( $text == 2 )
						{
							echo '<td width="100" bgcolor="yellow">';
							echo "WAIT"; 
 						   echo "</td>";
						
						}

						
						echo "</td><td width=\"100\">$r[2]% Complete&nbsp;</td>";
						echo "</tr>";			
					}
		
				}// end inner for-loop
			
			}// end for-loop		
		
			echo '</table>';
			
			$sql = " ";

			$close = pg_close( $con );

		}// end function create_sub_table()




		function Get_Tests( $params, $SQL, $SUBCAT, $CAT )
		{
			// Variables
			$ROWTOTAL = 0;
			
			echo '<tr>';
			echo '<td class="rowheader" colspan="14">';
			echo 'CATEGORY:  ' . $CAT;
			echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			echo 'SUB CATEGORY:  ' . $SUBCAT;
			echo '</td>';
			echo '</tr>';
			
			$conn = @pg_connect( $params ); 
			
			$result = @pg_exec( $conn, $SQL );
			
			$dbname = @pg_dbname( $conn );

			if( !$result )
			{
				echo '<br/>Error connecting to the database.<br/>';
			}

			$num = @pg_numrows( $result );
			

			for( $w = 0; $w < $num; $w++ )
			{
				$total_row = 0;

				$r = @pg_fetch_row( $result, $w );

				for( $i = 1; $i < ( count( $r ) - 3 ); $i++ )
				{
					if( $r[$i] )
					{
						++$total_row;
					}
				}

				if( $total_row > $this->$ROWTOTAL )
				{
					$this->$ROWTOTAL = $total_row;
				}

			}


			for( $i = 0; $i < $num; $i++ )
			{	
				$r = @pg_fetch_row( $result, $i );

				echo '<tr>';
				for( $k = 1; $k <= $this->$ROWTOTAL; $k++ )
				{
					echo "<td class=\"row\" width=\"25\">";
					echo "$r[$k]&nbsp;";
					echo "</td>";
				}
					
				if( $r[12] == 0 )
				{
					echo "<td class=\"rowfail\">";
					echo "FAIL";
					echo "</td>";
				}
				
				if( $r[12] == 1 )
				{
					echo "<td class=\"rowpass\">";
					echo "Pass";
					echo "</td>";
				}
				
				if( $r[12] == 2 )
				{
					echo "<td class=\"rowwait\">";
					echo "WAIT";
					echo "</td>";
				}
			
				
				echo '<td class="row">';
				echo "<a href=\"edit_test_table_data.php?postgres=$dbname&ID=$r[0]\" class=\"row\">EDIT TEST</a>";
				echo '</td>';
								
				echo '<td class="row">';
				echo "<a href=\"$r[14]\" class=\"row\">Test Case</a>";
				echo '</td>';

				echo '<td class="row">';
				echo "<a href=\"view_test_record_data.php?ID=$r[0]&DBNAME=$dbname\" class=\"row\">Results</a>";
				echo '</td>';
		
				echo '<td class="row">';
				echo "<a href=\"enterdata.php?ID=$r[0]&cat=$CAT&subcat=$SUBCAT\" class=\"row\">Pass/Fail</a>";
				echo '</td>';
			
				echo '</tr>';

			}
			

		}// end function Get_Tests()



		

		function Get_Records( $params, $SQL )
		{
			echo '<tr><td class="rowheader" colspan="13">';
			echo 'TEST RECORD(S)';
			echo '</td></tr>';

			$conn = @pg_connect( $params ); 
			$result = @pg_exec( $conn, $SQL );
			
			if( !$result )
			{
				echo '<br/>Error connecting to the database.<br/>';
			}
	
	
			echo '<tr>';
			echo '<td class="rowheader">Card</td>';
			echo '<td class="rowheader">Spin</td>';
			echo '<td class="rowheader">Firmware</td>';
			echo '<td class="rowheader">Bootprom</td>';
			echo '<td class="rowheader">FPGA</td>';
			echo '<td class="rowheader">Bug</td>';
			echo '<td class="rowheader">Engineer</td>';
			echo '<td class="rowheader">Status</td>';
			echo '<td class="rowheader">Edit</td>';
			echo '</tr>';
			
			$num = @pg_numrows( $result );
	
			for( $i = 0; $i < $num; $i++ )
			{	
				echo '<tr>';
				$r = @pg_fetch_row( $result, $i );
	
				for( $m = 0; $m < 7; $m++ )
				{
					echo '<td class="row">';
					echo "$r[$m]&nbsp;";
					echo '</td>';
				}
	

				if( $r[7] == 1 )
				{
					echo '<td class="rowpass" width="15">';
					echo "Pass";
					echo '</td>';
				}
				
				if( $r[7] == 0 )
				{
					echo '<td class="rowfail" width="15">';
					echo "Fail";
					echo '</td>';
				}
				
				if( $r[7] == 2 )
				{
					echo '<td class="rowwait" width="15">';
					echo "Wait";
					echo '</td>';
				}


				echo '<td class="row" width="15">';
				echo "<a href=\"\" class=\"row\">Edit</a>";
				echo '</td>';
	
				echo '</tr>';
			}
	

		}// end function Get_Records()

	}// end class dbConnect()

 	
// eof	
?>